<?php
/**
 * Controller.php
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@mygem.org so we can send you a copy immediately.
 *
 * @copyright   Copyright (c) 2012, MyGem <info@mygem.org>
 * @package     MyGem
 */


class MyGem_Controller
{

    // used in the case where render() is not called
    protected $_renderedView = false;

    protected $_requestQueries = array();

    protected $_paths = array();

    protected $_configs = array();

    protected $view;

    protected $action = 'index';

    public function __construct()
    {
        $this->view = new MyGem_View();
    }

    public function setupConfig()
    {
    }

    public function init()
    {
    }

    public function render($controller = null, $action = null, $raw = false)
    {
        $this->_renderedView = true;

        if ($controller === null) {
            $controller = isset($this->_requestQueries['c'])?$this->_requestQueries['c']:'index';
        }
        if ($action === null) {
            $action = isset($this->_requestQueries['a'])?$this->_requestQueries['a']:$this->action;
        }
        $fileContent = $this->_paths['views'] . DIRECTORY_SEPARATOR . $controller
                     . DIRECTORY_SEPARATOR . $action . '.php';

        $output = $this->view->generate($fileContent);
        if ($raw) return $output;
        else echo $output;
    }

    public function isViewRendered()
    {
        return $this->_renderedView;
    }

    public function setRequestQueries($requestQueries)
    {
        $this->_requestQueries = $requestQueries;
    }

    public function setPaths($paths)
    {
        $this->_paths = $paths;
        $this->view->setViewPath($this->_paths['views'] . DIRECTORY_SEPARATOR);
    }

    public function setAction($action)
    {
        $this->action = $action;
    }

    public function getAction()
    {
        return $this->action;
    }

    public function errorAction()
    {
    }

    public function indexAction()
    {
    }

}

